package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.PermissionMapper;
import com.atguigu.ssyx.acl.mapper.RolePermissionMapper;
import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.acl.utils.PermissionHelper;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public List<Permission> queryAllMenu() {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("CAST(id as signed)");
        List<Permission> permissionList = baseMapper.selectList(wrapper);

        return PermissionHelper.buildPermission(permissionList);
    }

    @Override
    public boolean removeChildById(Long id) {
        List<Long> idList = new ArrayList<>();
        this.selectChildById(id, idList);
        idList.add(id);
        baseMapper.deleteBatchIds(idList);
        return true;
    }

    @Override
    public Map<String, Object> getPermissionByRoleId(Long roleId) {
        HashMap<String, Object> result = new HashMap<>();

        // 1、查所有菜单
        List<Permission> allPermissionList = this.queryAllMenu();

        // 2、查该角色分配的所有菜单
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        List<RolePermission> rolePermissionList = rolePermissionMapper.selectList(wrapper);

        List<Long> rolePermissionIdList = rolePermissionList.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());

        List<Permission> assignPermissionList = allPermissionList.stream().filter(item -> rolePermissionIdList.contains(item.getId())).collect(Collectors.toList());

        //所有菜单列表
        result.put("allPermissionList",allPermissionList);
        //角色分配菜单列表
        result.put("assignPermission",PermissionHelper.buildPermission(assignPermissionList));
        return result;
    }

    @Override
    public void saveAdminRole(Long roleId, Long[] permissionId) {
        // 1、先删除之前分配的
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        rolePermissionService.remove(wrapper);

        // 2、批量保存
        List<RolePermission> list = new ArrayList<>();

        for (Long l : permissionId) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(l);
            rolePermission.setRoleId(roleId);
            list.add(rolePermission);
        }

        rolePermissionService.saveBatch(list);
    }

    private void selectChildById(Long id, List<Long> idList) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id).select("id");
        List<Permission> childList = baseMapper.selectList(wrapper);
        childList.forEach(item -> {
            idList.add(item.getId());
            this.selectChildById(item.getId(), idList);
        });
    }
}
